package com.mySampleApplication.client.components;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.HLayout;

public class SFilterForm {
    public static DynamicForm getFilterForm(DataSource dataSource, ListGrid listGrid){
        final DynamicForm filterForm = new DynamicForm();
        filterForm.setIsGroup(true);
        filterForm.setGroupTitle("Search");
        filterForm.setNumCols(6);
        filterForm.setHeight(80);
        filterForm.setDataSource(dataSource);
        filterForm.setAutoFocus(false);

        TextItem NameItem = new TextItem("name");
        TextItem LoginNameItem = new TextItem("loginName");
//        SelectItem statusItem = new SelectItem("gender");
//        statusItem.setOperator(OperatorId.EQUALS);
//        statusItem.setAllowEmptyValue(true);


        filterForm.setFields(NameItem, LoginNameItem);
        //值改变触发搜索
/*        filterForm.addItemChangedHandler(new ItemChangedHandler() {
            public void onItemChanged(ItemChangedEvent event) {
                listGrid.fetchData(filterForm.getValuesAsCriteria());
            }
        });*/

        return filterForm;
    }
    public static HLayout  getSerchButton(ListGrid listGrid,DynamicForm filterForm){
        HLayout hLayout = new HLayout(10);
        hLayout.setHeight(22);

        IButton filterButton = new IButton("Filter");
        filterButton.setIcon("[SKIN]/actions/search.png"); // 设置图标路径
        filterButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                listGrid.fetchData(filterForm.getValuesAsCriteria());
            }
        });
        filterButton.setAutoFit(true);

        IButton clearButton = new IButton("Clear");
        clearButton.setIcon("[SKIN]/actions/refresh.png"); // 设置图标路径
        clearButton.setAutoFit(true);
        clearButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                listGrid.fetchData();
                filterForm.clearValues();
            }
        });

        hLayout.addMember(filterButton);
        hLayout.addMember(clearButton);
        return hLayout;
    }
}
